package com.das.mechanic_base.adapter.create;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.arch.core.internal.SafeIterableMap;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3MyApp;
import com.das.mechanic_base.bean.main.DetectionNewBean;
import com.das.mechanic_base.mvp.view.show.X3ShowImageActivity;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.hjq.toast.ToastUtils;
import com.kproduce.roundcorners.RoundImageView;
import com.kproduce.roundcorners.RoundTextView;

import java.util.ArrayList;
import java.util.List;

import cn.jzvd.JZVideoPlayerStandard;
import uk.co.senab.photoview.PhotoView;

public class CarBodyAdapter extends RecyclerView.Adapter<CarBodyAdapter.CarBodyHolder> {

    private List<DetectionNewBean.QuestionToItemDetectionBean> mList;

    private Context mContext;

    private int currentPos;

    public CarBodyAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
    }

    public void changeCarBody(List<DetectionNewBean.QuestionToItemDetectionBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public CarBodyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CarBodyHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_car_body_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull CarBodyHolder holder, int position) {
        currentPos = position;
        List<DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean> questionList = mList.get(position).questionList;

        String content = "";

        if (!X3StringUtils.isListEmpty(questionList)) {
            for (DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean listBean : questionList) {
                if (X3StringUtils.isEmpty(listBean.answer)) {
                    continue;
                }
                String[] answer = listBean.answer.split("\\. ");
                if (answer == null || answer.length <= 1) {
                    continue;
                }
                if (X3StringUtils.isEmpty(content)) {
                    content = answer[1];
                }else {
                    content = content + ", " + answer[1];
                }
            }
        }

        holder.tv_title.setText(mList.get(position).itemName + ": " + content);

        MyPageAdapter myPageAdapter = new MyPageAdapter();
        myPageAdapter.imageList = mList.get(position).itemUrlList;
        holder.vp_image.setAdapter(myPageAdapter);
        try {
            if (mList.get(position).itemUrlList.size() > 0){
                holder.rtv_num_index.setText("1/"+mList.get(position).itemUrlList.size());
            } else {
                holder.rtv_num_index.setText("0/"+mList.get(position).itemUrlList.size());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        holder.vp_image.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int pos) {
                try {
                    holder.rtv_num_index.setText((pos + 1) + "/"+mList.get(position).itemUrlList.size());
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    IOnCarBodyEdit iOnCarBodyEdit;

    public CarBodyAdapter setiOnCarBodyEdit(IOnCarBodyEdit iOnCarBodyEdit) {
        this.iOnCarBodyEdit = iOnCarBodyEdit;
        return this;
    }

    public interface IOnCarBodyEdit {
        void iOnCarBodyEdit(String sn,List<String> path,List<String> selectCarList);

        // 删除
        void iOnCarBodyDelete(List<DetectionNewBean.QuestionToItemDetectionBean> mList);

        /**
         * 预览图片
         * @param imageUrl
         */
        void iOnReviewImage(List<String> imageUrl,int index);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public List<DetectionNewBean.QuestionToItemDetectionBean> getmList() {
        return mList;
    }

    class CarBodyHolder extends RecyclerView.ViewHolder {
        ViewPager vp_image;
        TextView tv_title;
        RoundTextView rtv_num_index;
        RoundTextView rv_del;
        RoundTextView rv_edit;
        public CarBodyHolder(@NonNull View itemView) {
            super(itemView);
            vp_image = itemView.findViewById(R.id.vp_image);
            tv_title = itemView.findViewById(R.id.tv_title);
            rv_del = itemView.findViewById(R.id.rv_del);
            rv_edit = itemView.findViewById(R.id.rv_edit);
            rtv_num_index = itemView.findViewById(R.id.rtv_num_index);

            rv_del.setOnClickListener(v -> {
                mList.remove(getAbsoluteAdapterPosition());
                notifyItemRemoved(getAbsoluteAdapterPosition());
                notifyItemChanged(getAbsoluteAdapterPosition(),mList.size() - getAbsoluteAdapterPosition());
                if (iOnCarBodyEdit != null) {
                    iOnCarBodyEdit.iOnCarBodyDelete(mList);
                }
            });

            rv_edit.setOnClickListener(v -> {
                List<DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean> questionList = mList.get(getAbsoluteAdapterPosition()).questionList;
                List<String> sList = new ArrayList<>();
                if (!X3StringUtils.isListEmpty(questionList)) {
                    for (DetectionNewBean.QuestionToItemDetectionBean.QuestionListBean questionListBean : questionList) {
                        sList.add(questionListBean.answer);
                    }
                }

                if (iOnCarBodyEdit != null) {
                    iOnCarBodyEdit.iOnCarBodyEdit(mList.get(getAbsoluteAdapterPosition()).sn,mList.get(getAbsoluteAdapterPosition()).itemUrlList,sList);
                }
            });

        }
    }

    private List<JZVideoPlayerStandard> mJzList = new ArrayList<>();

      class MyPageAdapter extends PagerAdapter {
          public  List<String>  imageList ;

        @Override
        public int getCount() {
            return imageList.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            View view = View.inflate(mContext, R.layout.x3_alone_image_item,null);
            ImageView iv_img = view.findViewById(R.id.iv_image);
            iv_img.setScaleType(ImageView.ScaleType.FIT_XY);
            JZVideoPlayerStandard jz_view = view.findViewById(R.id.jz_view);

            mJzList.add(jz_view);
            String path = imageList.get(position);
            if (!X3StringUtils.isEmpty(path) && !path.contains("storage")) {
                path = X3StringUtils.getImageUrl(path);
            }

            if (path.endsWith("mp4")) {
                iv_img.setVisibility(View.GONE);
                jz_view.setVisibility(View.VISIBLE);
                X3GlideNewUtils.loadNormalNoCropImage(mContext, path,jz_view.thumbImageView, Color.BLACK);
                jz_view.backButton.setVisibility(View.GONE);
                jz_view.setUp((path.contains("http://")) ? X3MyApp.getInstance().getVideoCacheProxy(mContext).getProxyUrl(path) : path,JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL,"");
                jz_view.startVideo();

            }else {
                iv_img.setVisibility(View.VISIBLE);
                jz_view.setVisibility(View.GONE);
                X3GlideNewUtils.loadNormalNoCropImage(mContext, path,iv_img,Color.BLACK);
            }

            iv_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iOnCarBodyEdit != null) {
                        try {
                            iOnCarBodyEdit.iOnReviewImage(mList.get(currentPos).itemUrlList,position);
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            });

            jz_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iOnCarBodyEdit != null) {
                        try {
                            iOnCarBodyEdit.iOnReviewImage(mList.get(currentPos).itemUrlList,position);
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            });

            container.addView(view);

            return view;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }
    }


}
